package com.xcy.goatmilk.service.impl;

import com.github.pagehelper.PageInfo;
import com.xcy.goatmilk.base.BaseService;
import com.xcy.goatmilk.mapper.IncomeMapper;
import com.xcy.goatmilk.pojo.Income;
import com.xcy.goatmilk.pojo.qo.IncomeQo;
import com.xcy.goatmilk.service.IncomeService;
import com.xcy.goatmilk.table.IncomeTableData;
import com.xcy.goatmilk.util.CollectionsUtils;
import com.xcy.goatmilk.util.DateUtils;
import com.xcy.goatmilk.wrapper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xcy
 * @Desc
 * @date 2020/7/13 16:33
 * @Version v1.0
 */
@Service
public class IncomeServiceImpl extends BaseService implements IncomeService {

    @Autowired
    private IncomeMapper incomeMapper;


    @Override
    public Page listByDay(IncomeQo qo) {

        Integer days = qo.getDays();

        super.startPage(qo);

        Date now = new Date();
        Date date = DateUtils.addDays(now, -days + 1);
        Date todayStartTime = DateUtils.getStartTime(date);
        Date todayEndTime = DateUtils.getEndTime(now);
        String dayStart = DateUtils.parse2LongString(todayStartTime);
        String dayEnd = DateUtils.parse2LongString(todayEndTime);
        qo.setStartDate(dayStart);
        qo.setEndDate(dayEnd);
        List<IncomeTableData> pageList = incomeMapper.selectIncome(qo);

        PageInfo<IncomeTableData> pageInfo = new PageInfo<>(pageList);
        return super.wrap(pageInfo);

    }

    @Override
    public Page listByWeek(IncomeQo qo) {

        Integer weeks = qo.getWeeks();
        Map countMap = new HashMap<String, Integer>();
        for (int i = 0; i < weeks; i++) {
            //weeks = i = 0 表示查本周
            String thisWeekStart = DateUtils.getWeekStart();
            String thisWeekEnd = DateUtils.getWeekEnd();
            //本周订单数量
            if (i == 0) {
                qo.setStartDate(thisWeekStart);
                qo.setEndDate(thisWeekEnd);

                List<IncomeTableData> pageList = incomeMapper.selectIncomeByWeek(qo);

                countMap.put(i + "||" + thisWeekStart + "-" + thisWeekEnd, pageList);
            } else {
                //第几周前的开始时间,结束时间.
                String weekStartBeforeN = DateUtils.getWeekStartBeforeN(i, thisWeekStart);
                String weekEndBeforeN = DateUtils.getWeekEndBeforeN(i, thisWeekEnd);
                qo.setStartDate(weekStartBeforeN);
                qo.setEndDate(weekEndBeforeN);
                List<IncomeTableData> pageList = incomeMapper.selectIncomeByWeek(qo);
                countMap.put(i + "||" + weekStartBeforeN + "-" + thisWeekEnd, pageList);
            }
        }
        List countList = CollectionsUtils.mapToListAndSortByKey(countMap);

        PageInfo<IncomeTableData> pageInfo = new PageInfo<>(countList);
        return super.wrap(pageInfo);
    }

    @Override
    public Page listByMonth(IncomeQo qo) {
        Integer months = qo.getMonths();
        Map countMap = new HashMap<String, Integer>();
        String monthStart = DateUtils.getMonthStart(months - 1);
        String monthEnd = DateUtils.getMonthEnd(months - 1);
        qo.setStartDate(monthStart);
        qo.setEndDate(monthEnd);
        List<IncomeTableData> pageList = incomeMapper.selectIncomeByMonth(qo);
        countMap.put(monthStart, pageList);
        List countList = CollectionsUtils.mapToListAndSortByKey(countMap);
        PageInfo<IncomeTableData> pageInfo = new PageInfo<>(countList);
        return super.wrap(pageInfo);
    }

    @Override
    public Page listByYear(IncomeQo qo) {
        Integer years = qo.getYears();
        Map countMap = new HashMap<String, Integer>();
        String yearStartTime = DateUtils.getYearStartTimeBeforeN(years - 1);
        String yearEndTime = DateUtils.getYearEndTimeBeforeN(years - 1);
        qo.setStartDate(yearStartTime);
        qo.setEndDate(yearEndTime);
        List<IncomeTableData> pageList = incomeMapper.selectIncomeByYear(qo);
        countMap.put(yearStartTime, pageList);
        List countList = CollectionsUtils.mapToListAndSortByKey(countMap);
        PageInfo<IncomeTableData> pageInfo = new PageInfo<>(countList);
        return super.wrap(pageInfo);
    }

    @Override
    public void add(Income income) {
        if (income.getDate() == null) {
            income.setDate(new Date());
        }
        income.setNetIncome(income.getIncome() - income.getExpend());
        incomeMapper.insert(income);
    }


}
